import { Cart } from 'cart-model.js';

var cart = new Cart();

Page({
	data:{
		carts: [],
		minusStatuses: [],
		selectedAllStatus:'',
		total: '',
		startX: 0,
		itemLefts: []
	},
  onLoad: function (options) {
    this._loadData();
    console.log(this.data.carts)
  },
  _loadData: function (callback) {
    var that = this;
    //获得product信息
    cart.getCartData((data) => {
    
      that.setData({
        carts: data,
        selectedAllStatus: data[0]['selectAll']
    });
      if(data)
      {
        wx.showNavigationBarLoading();
      }
    });

  },
  onShow:function()
  {
    var that = this;
    //获得product信息
    cart.getCartData((data) => {

      that.setData({
        carts: data,
        selectedAllStatus: data[0]['selectAll']
      });
      wx.hideNavigationBarLoading();
    }); 
    var carts = this.data.carts;
    // 计算总金额
    var total = 0;
    for (var i = 0; i < carts.length; i++) {
      if (carts[i]['selected']) {
        total += carts[i]['num'] * carts[i]['pspeci_price'];
      }
    }
    total = total.toFixed(2);
    // 写回经点击修改后的数组
    this.setData({
      carts: carts,
      total: total
    });
  },
  showGoods: function (e) {
    // 点击购物车某件商品跳转到商品详情
    var objectId = e.currentTarget.dataset.objectId;
    wx.navigateTo({
      url: '../goods/detail/detail?objectId=' + objectId
    });
  },
  bindCheckbox: function (e) {
    // wx.showLoading({
    // 	title: '操作中',
    // 	mask: true
    // });
    /*绑定点击事件，将checkbox样式改变为选中与非选中*/
    //拿到下标值，以在carts作遍历指示用
    var index = parseInt(e.currentTarget.dataset.index);
    var cartId = parseInt(e.currentTarget.dataset.cartid);
    //原始的icon状态
    var selected = this.data.carts[index]['selected'];
    var carts = this.data.carts;
    // 对勾选状态取反
    carts[index]['selected']=!selected;
   
    // 写回经点击修改后的数组
    this.setData({
      carts: carts,
    });
    // update database
    cart.getCartId(cartId,(data) => {
      wx.hideLoading();
    });
    this.sum();
    for (var i = 0; i < carts.length;i++){
      if (carts[i]['selected']==false){
        this.setData({
          selectedAllStatus: false,
        });
        return;
      }else{
        this.setData({
          selectedAllStatus: true,
        });
      }
    }
  },
  
  bindCheckout: function () {
    
    var cartIds = this.calcIds();
    console.log(cartIds);
    var cartId = JSON.stringify(cartIds);
    console.log(cartId);
    if (cartIds.cartIds.length <= 0) {
     
      wx.showToast({
        title: '请勾选商品',
        icon: 'success',
        duration: 1000
      })
    }else{
      wx.navigateTo({
        url: '../../../../order/checkout/checkout?cartIds=' + cartId,
      });
    }
  },
  sum: function () {
    var carts = this.data.carts;
    // 计算总金额
    var total = 0;
    for (var i = 0; i < carts.length; i++) {
      if (carts[i]['selected']) {
        total += carts[i]['num'] * carts[i]['pspeci_price'];
      }
    }
    total = total.toFixed(2);
    // 写回经点击修改后的数组
    this.setData({
      carts: carts,
      total: total
    });
  },
  bindMinus: function (e) {
    // loading提示
    // wx.showLoading({
    // 	title: '操作中',
    // 	mask: true
    // });
    var that =this;
    var index = parseInt(e.currentTarget.dataset.index);
    var num = that.data.carts[index]['num'];
    var id = e.currentTarget.dataset.id;
    // 如果只有1件了，就不允许再减了
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 购物车数据
    var carts = that.data.carts;
    carts[index]['num'] = num;
    // 按钮可用状态
    var minusStatuses = that.data.minusStatuses;
    minusStatuses[index] = minusStatus;
    // 将数值与状态写回
    
    // update database
    cart.minus(id, (data) => {
      that.setData({
        carts: carts,
        minusStatuses: minusStatuses
      });
      wx.hideLoading();
    });
    that.sum();
  },
  bindPlus: function (e) {
    // wx.showLoading({
    // 	title: '操作中',
    // 	mask: true
    // });
    var index = parseInt(e.currentTarget.dataset.index);
    var num = this.data.carts[index]['num'];
    var id = e.currentTarget.dataset.id;
    // 自增
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 购物车数据
    var carts = this.data.carts;
    carts[index]['num']= num;
    // 按钮可用状态
    var minusStatuses = this.data.minusStatuses;
    minusStatuses[index] = minusStatus;
    // 将数值与状态写回
    this.setData({
      carts: carts,
      minusStatuses: minusStatuses
    });
    // update database
    cart.plus(id, (data) => {
      wx.hideLoading();
    });
    this.sum();
  },
  bindManual: function (e) {
    // wx.showLoading({
    // 	title: '操作中',
    // 	mask: true
    // });
    var index = parseInt(e.currentTarget.dataset.index);
    var carts = this.data.carts;
    var num = parseInt(e.detail.value);
    carts[index]['num']= num;
    // 将数值与状态写回
    this.setData({
      carts: carts
    });
    /*carts[index].save().then(function () {
      wx.hideLoading();
    });*/
    this.sum();
  },
  calcIds: function () {
    // 遍历取出已勾选的cid
    // var buys = [];
    var cartIds = [];
    var nums=[];
    
    for (var i = 0; i < this.data.carts.length; i++) {
      if (this.data.carts[i]['selected']) {
        // 移动到Buy对象里去
        // cartIds += ','
        cartIds.push(this.data.carts[i]['cart_id']);
        nums.push(this.data.carts[i]['num']);
      }
    }
    var alls={};
    alls['cartIds'] = cartIds;
    alls['nums']=nums;    
    return alls;
  },
  bindSelectAll: function () {
    // wx.showLoading({
    // 	title: '操作中',
    // 	mask: true
    // });

    // 环境中目前已选状态
    var selectedAllStatus = this.data.selectedAllStatus;
    // 取反操作
    selectedAllStatus = !selectedAllStatus;
    // 购物车数据，关键是处理selected值
    var carts = this.data.carts;
    // 遍历
    for (var i = 0; i < carts.length; i++) {
      carts[i]['selected']= selectedAllStatus;
      // update selected status to db
    }
    /*
    AV.Object.saveAll(carts).then(function (objects) {
      wx.hideLoading();
    }, function (error) {
      // 异常处理
    });
    */
    this.setData({
      selectedAllStatus: selectedAllStatus,
      carts: carts,
    });
    this.sum();
  },
  touchStart: function (e) {
    var startX = e.touches[0].clientX;
    this.setData({
      startX: startX,
      itemLefts: []
    });
  },
  touchMove: function (e) {
    var index = e.currentTarget.dataset.index;
    var movedX = e.touches[0].clientX;
    var distance = this.data.startX - movedX;
    var itemLefts = this.data.itemLefts;
    itemLefts[index] = -distance;
    this.setData({
      itemLefts: itemLefts
    });
  },
  touchEnd: function (e) {
    var index = e.currentTarget.dataset.index;
    var endX = e.changedTouches[0].clientX;
    var distance = this.data.startX - endX;
    // button width is 60
    var buttonWidth = 150;
    if (distance <= 0) {
      distance = 0;
    } else {
      if (distance >= buttonWidth) {
        distance = buttonWidth;
      } else if (distance >= buttonWidth / 2) {
        distance = buttonWidth;
      } else {
        distance = 0;
      }
    }
    var itemLefts = this.data.itemLefts;
    itemLefts[index] = -distance;
    this.setData({
      itemLefts: itemLefts
    });
  },
  delete:function(e)
  {
    var that=this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    cart.delete(id, (data) => {
    that.data.carts.splice(index,1);
      that.setData({
        carts: that.data.carts,
      });
      var itemLefts = this.data.itemLefts;
      //console.log("itemLefts" + itemLefts);
      itemLefts[index] = 0;
      this.setData({
        itemLefts: itemLefts
      });
      console.log(that.data.carts);
    });
    // 计算总金额
    var total = 0;
    for (var i = 0; i < that.data.carts.length; i++) {
      if (that.data.carts[i]['selected']) {
        total += that.data.carts[i]['num'] * that.data.carts[i]['pspeci_price'];
      }
    }
    total = total.toFixed(2);
    // 写回经点击修改后的数组
    this.setData({
      carts: that.data.carts,
      total: total
    });
  }
 /* delete: function (e) {
    var that = this;
    // 购物车单个删除
    var objectId = e.currentTarget.dataset.objectId;
    console.log(objectId);
    wx.showModal({
      title: '提示',
      content: '确认要删除吗',
      success: function (res) {
        if (res.confirm) {
          // 从网络上将它删除
          var cart = AV.Object.createWithoutData('Cart', objectId);
          cart.destroy().then(function () {
            // 成功
            wx.showToast({
              title: '删除成功',
              icon: 'success',
              duration: 1000
            });
            that.reloadData();
            that.setData({
              itemLefts: []
            });
          }, function (error) {
            // 异常处理
          });
        }
      }
    })
  },*/
})